package com.example.service;

import com.example.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 订单服务类
 * 演示另一种调用模式
 */
@Service
public class OrderService {

    @Autowired
    private UserService userService;

    /**
     * 创建订单
     * 直接调用RedisUtil.set
     */
    public void createOrder(String orderId, String orderInfo) {
        String key = "order:" + orderId;
        RedisUtil.set(key, orderInfo);

        // 同时更新用户状态
        String userId = extractUserIdFromOrder(orderInfo);
        userService.updateUserStatus(userId, "has_order");
    }

    /**
     * 取消订单
     * 通过UserService间接调用RedisUtil.set
     */
    public void cancelOrder(String orderId) {
        String orderInfo = getOrderInfo(orderId);
        String userId = extractUserIdFromOrder(orderInfo);
        userService.updateUserStatus(userId, "no_order");
    }

    private String extractUserIdFromOrder(String orderInfo) {
        // 模拟从订单信息中提取用户ID
        return "user_" + orderInfo.hashCode();
    }

    private String getOrderInfo(String orderId) {
        // 模拟获取订单信息
        return "order_info_for_" + orderId;
    }
}
